package com.brillilab.starter.controller.aio.method;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.method.MethodTypeLevelEnum;
import com.brillilab.domain.po.method.MethodType;
import com.brillilab.domain.po.user.Users;
import com.brillilab.domain.vo.method.MethodTypePrefixEdit;
import com.brillilab.domain.vo.method.MethodTypeVo;
import com.brillilab.domain.vo.method.RecommendMethodTypeVo;
import com.brillilab.domain.vo.topics.NewMethodTypeVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.user.IUsersService;
import com.brillilab.service.logic.method.MethodTypeLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wuzhiyu
 * @Title: MethodTypeController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/2/2110:03
 * 二级类型和实验类型限制64字符
 */
@RestController
@RequestMapping("/api/method/type")
public class MethodTypeController {

    @Resource
    private UsersLogic usersLogic;
    @Resource
	private IUsersService usersService;
    @Resource
    private MethodTypeLogic methodTypeLogic;

    /**
     * 新增类型 (类型名称不能相同)
     *
     * @param add
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/add", method=RequestMethod.POST)
    public ResponseVo addNewMethodType(@RequestBody MethodTypeVo add,@RequestHeader(StarterConstant.TOKEN) String token) {
        Assert.isTrue(add != null && add.getLevel() != null && add.getParentId() != null
                && StringUtils.isNotBlank(add.getName()) && StringUtils.isNotBlank(token),"请求参数缺失！");
        if(MethodTypeLevelEnum.LevelThird.getLevel().equals(add.getLevel())) {
            Assert.isTrue(add.getName().contains(":"),"名称不符合规范！");
            Assert.isTrue(add.getName().split(":").length == 2,"名称不符合规范！");
        }
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        MethodTypeVo method=methodTypeLogic.addNewMethodType(add,userInfo);
        return ResponseVo.success(method);
    }

    /**
     * 修改类型
     *
     * @param edit
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/edit", method=RequestMethod.POST)
    public ResponseVo editMethodType(@RequestBody MethodTypeVo edit,@RequestHeader(StarterConstant.TOKEN) String token) {
        Assert.isTrue(edit != null && edit.getId() != null && edit.getLevel() != null && StringUtils.isNotBlank(token),"请求参数缺失！");
        if(MethodTypeLevelEnum.LevelThird.getLevel().equals(edit.getLevel())) {
            Assert.isTrue(edit.getName().contains(":"),"名称不符合规范！");
            Assert.isTrue(edit.getName().split(":").length == 2,"名称不符合规范！");
        }
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        MethodTypeVo method=methodTypeLogic.editMethodType(edit,userInfo);
        return ResponseVo.success(method);
    }

    /**
     * 三级类型前缀修改
     *
     * @param edit
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/editPrefix", method=RequestMethod.POST)
    public ResponseVo editThirdMethodTypePrefix(@RequestBody MethodTypePrefixEdit edit,@RequestHeader(StarterConstant.TOKEN) String token) {
        Assert.isTrue(edit != null && CollectionUtils.isNotEmpty(edit.getIds()) &&
                edit.getSecondTypeId() != null && StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        methodTypeLogic.editThirdMethodTypePrefix(edit,userInfo);
        return ResponseVo.success();
    }

    /**
     * 删除类型
     *
     * @param id
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/delete", method=RequestMethod.DELETE)
    public ResponseVo deleteMethodType(Long id,@RequestHeader(StarterConstant.TOKEN) String token) {
        Assert.isTrue(id != null && StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        methodTypeLogic.deleteMethodType(id,userInfo);
        return ResponseVo.success("删除成功！");
    }

    /**
     * 删除三级方法类型前缀
     * @param delete
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/deletePrefix", method=RequestMethod.POST)
    public ResponseVo deleteThirdMethodTypePrefix(@RequestBody MethodTypePrefixEdit delete,@RequestHeader(StarterConstant.TOKEN) String token) {
        Assert.isTrue(CollectionUtils.isNotEmpty(delete.getIds()) && StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        methodTypeLogic.deleteThirdMethodTypePrefix(delete.getIds(),userInfo);
        return ResponseVo.success("删除成功！");
    }

    /**
     * 分级方法类型列表
     * TODO methodNum
     * @param level
     * @param token
     * @return
     */
    @RequestMapping(value="/level/list", method=RequestMethod.GET)
    public ResponseVo methodTypeList(Integer level,Long parentId,@RequestHeader(StarterConstant.TOKEN) String token) {
        Assert.isTrue(level != null && StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        List<MethodType> methodTypes=methodTypeLogic.getMethodTypeList(level,parentId,userInfo);
        return ResponseVo.success(methodTypes);
    }

    /**
     * 所有方法类型
     * TODO methodNum
     * @param token
     * @return
     */
    @RequestMapping(value="/all",method=RequestMethod.GET)
    public ResponseVo allMethodTypeList(@RequestHeader(StarterConstant.TOKEN) String token){
        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        List<NewMethodTypeVo> rs = methodTypeLogic.getAllMethodTypeMap(userInfo.getId());
        return ResponseVo.success(rs);
    }
    
    /**
     * 根据labMemberId获取方法列表
     * @param token
     * @return
     */
    @RequestMapping(value="/allByMemberId",method=RequestMethod.GET)
    public ResponseVo allMethodTypeListByMemberId(@RequestHeader(StarterConstant.TOKEN) String token, Long labMemberId){
    	Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
    	UserInfoVo userInfo=usersLogic.getUserByToken(token);
    	Users user = usersService.selectUser(userInfo.getLabId(), labMemberId, null);
        List<NewMethodTypeVo> rs = methodTypeLogic.getAllVisibleMethodTypeMap(user.getId());
    	return ResponseVo.success(rs);
    }

    /**
     * 方法类型隐藏
     *
     * @param id
     * @param visible
     * @param token
     * @return
     */
    @RequestMapping(value="/hide", method=RequestMethod.GET)
    public ResponseVo hideMethodType(Long id,Integer visible,@RequestHeader(StarterConstant.TOKEN) String token) {
        Assert.isTrue(id != null && visible != null && StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        methodTypeLogic.hideMethodType(id,visible,userInfo);
        return ResponseVo.success();
    }

    @RequestMapping(value="/hidePrefix", method=RequestMethod.POST)
    public ResponseVo hideThirdMethodTypePrefix(@RequestBody MethodTypePrefixEdit hide,@RequestHeader(StarterConstant.TOKEN) String token) {
        Assert.isTrue(CollectionUtils.isNotEmpty(hide.getIds()) && hide.getVisible() != null && StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        methodTypeLogic.hideThirdMethodTypePrefix(hide.getIds(),hide.getVisible(),userInfo);
        return ResponseVo.success();
    }

    /**
     * 根据labMemberId获取方法类型推荐
     * TODO methodNum
     * @param token
     * @return
     */
    @RequestMapping(value="/recommendByMemberId")
    public ResponseVo recommendMethodTypeListByMemberId(@RequestHeader(StarterConstant.TOKEN) String token, Long labMemberId) {
        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        Users user = usersService.selectUser(userInfo.getLabId(), labMemberId, null);
        List<RecommendMethodTypeVo> recommend=methodTypeLogic.recommendMethodType(user.getId());
        return ResponseVo.success(recommend);
    }
    
    /**
     * 方法类型推荐
     * TODO methodNum
     * @param token
     * @return
     */
    @RequestMapping(value="/recommend")
    public ResponseVo recommendMethodTypeList(@RequestHeader(StarterConstant.TOKEN) String token) {
    	Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
    	UserInfoVo userInfo=usersLogic.getUserByToken(token);
    	List<RecommendMethodTypeVo> recommend=methodTypeLogic.recommendMethodType(userInfo.getId());
    	return ResponseVo.success(recommend);
    }

    /**
     * 参考方法类型列表
     * TODO methodNum
     * @return
     */
    @RequestMapping(value="/reference", method=RequestMethod.GET)
    public ResponseVo referenceMethodType() {
        List<MethodType> methodTypes=methodTypeLogic.getReferenceMethodType();
        List<MethodType> firstType=methodTypes.stream()
                .filter(methodType -> MethodTypeLevelEnum.LevelFirst.getLevel().equals(methodType.getLevel()))
                .collect(Collectors.toList());
        List<MethodType> secondType=methodTypes.stream()
                .filter(methodType -> MethodTypeLevelEnum.LevelSecond.getLevel().equals(methodType.getLevel()))
                .collect(Collectors.toList());
        Map<String,List> rs =new HashMap<>();
        rs.put("firstType",firstType);
        rs.put("secondType",secondType);
        return ResponseVo.success(rs);
    }

    @RequestMapping(value="/search", method=RequestMethod.GET)
    public ResponseVo methodTypeSearch(Long parentId ,Integer level ,String name, @RequestHeader(StarterConstant.TOKEN) String token){
        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        List<MethodType> methodTypes = methodTypeLogic.search(parentId,level,name,userInfo.getId());
        return ResponseVo.success(methodTypes);
    }
}
